function selectWebSiteForAds(obj){
	
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/change_website_profiles",
		dataType: "json",
        data: {
            website: $(obj).val()
        },
        success: function(data){
			$("#ad_category").html(data['category']);
			$("#website_cm_source_tag").val(data['cm_source_tag']);
        }
    });
}
function changeWebSite(obj){
	
	
    $.ajax({
        beforeSend: function(){
            $("#overview_table").html('<img src="/images/public/ajax-loader.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/summary/get_overview_table",
        data: {
            website: $(obj).val()
        },
        success: function(data){
            $("#overview_table").html(data);
			if ($(".tablesorter").length > 0) {
				$(".tablesorter").tablesorter({
					sortList: [[0, 0]],
					widgets: ['zebra']
				});
			}
        }
    });
}

function changeAdsWebSite(obj){
	
	
    $.ajax({
        beforeSend: function(){
            $("#overview_table").html('<img src="/images/public/ajax-loader.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/ads/get_ads_table",
        data: {
            website: $(obj).val()
        },
        success: function(data){
            $("#overview_table").html(data);
            
            if ($(".tablesorter").length > 0) {
                $(".tablesorter").tablesorter({
                    sortList: [[0, 0]],
                    widgets: ['zebra']
                });
            }
        }
    });
}


function getAccountSettings(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
		dataType: "json",
        url: "/summary/get_account_settings",
        success: function(data){
			/*
			 * assign css id
			 */
            $("#selected_menu_btn").removeAttr("id");
			$(obj).attr("id", "selected_menu_btn");
			
			/*
			 * remove subterm and reassign
			 */
			$('#subterms').remove();
			$("#account_settings_btn").append(data['account_settings_subterms']);
			$("#overview").html(data['personal_info']);
        }
    });
}

function getChangePassword(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/summary/get_change_password",
        success: function(data){
           	$(".link02").attr('class', 'link01');
          	$(obj).parent().attr('class', 'link02');
			$("#overview").html(data);
        }
    });
}

function changePassword(){
	$.ajax({
		 beforeSend: function(){
            $("#updatemsg").html('<img src="/images/public/ajax-loader-circle.gif" />');
        },
        type: "POST",
		dataType: "json",
        cache: false,
        url: "/summary/change_password",
		data: {
            old_password: $('#old_password').val(),
			new_password: $('#new_password').val(),
			confirm_new_password: $('#confirm_new_password').val()
        },
        success: function(data){
			console.log(data['msg']);
			console.log(data['status']);
			$("#updatemsg").html(data['msg']);
        }
    });
}

function getPersonalInfo(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/summary/get_personal_info",
        success: function(data){
			$(".link02").attr('class', 'link01');
          	$(obj).parent().attr('class', 'link02');
			$("#overview").html(data);
        }
    });
}

function savePersonalInfo(){
	$.ajax({
		 beforeSend: function(){
            $("#updatemsg").html('<img src="/images/public/ajax-loader-circle.gif" />');
        },
        type: "POST",
		dataType: "json",
        cache: false,
        url: "/summary/save_personal_info",
		data: {
            user_name: $('#user_name').val(),
			user_id: $('#user_id').val(),
			user_phone: $('#user_phone').val(),
			user_address: $("#user_address").val()
        },
        success: function(data){
			$("#updatemsg").html(data['msg']);
        }
    });
}

function getOverview(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/summary/get_overview",
        success: function(data){
            /*
             * remove subterms first
             */
			$('#subterms').remove();
			
			$("#selected_menu_btn").removeAttr("id");
			$(obj).attr("id", "selected_menu_btn");
			$(".child01").hide();
			$(obj).siblings('div').show();
			$("#overview").html(data);
			if ($(".tablesorter").length > 0) {
				$(".tablesorter").tablesorter({
					sortList: [[0, 0]],
					widgets: ['zebra']
				});
			}
        }
    });
}

function getAdsOverview(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/ads/get_ads",
        success: function(data){
            /*
             * remove subterms first
             */
			$('#subterms').remove();
			
			$("#selected_menu_btn").removeAttr("id");
			$(obj).attr("id", "selected_menu_btn");
			$(".child01").hide();
			$(obj).siblings('div').show();
			$("#overview").html(data);
			if ($(".tablesorter").length > 0) {
				$(".tablesorter").tablesorter({
					sortList: [[0, 0]],
					widgets: ['zebra']
				});
			}
            
        }
    });
}

function getNewAds(obj){
	$.ajax({
        beforeSend: function(){
            $("#overview").html('<img src="/images/public/ajax-loader-bar.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/ads/get_new_ads",
        success: function(data){
            /*
             * remove subterms first
             */
			$('#subterms').remove();
			
			$("#selected_menu_btn").removeAttr("id");
			$(obj).attr("id", "selected_menu_btn");
			$(".child01").hide();
			$(obj).siblings('div').show();
			$("#overview").html(data);
			if ($(".tablesorter").length > 0) {
				$(".tablesorter").tablesorter({
					sortList: [[0, 0]],
					widgets: ['zebra']
				});
			}
			
			$("#ads_preview").bind("click", function(){
                adsPreview();
				return false;
            });
			
			$("#ads_save").bind("click", function(){
                adsSave();
				return false;
            });
			
			$("#ads_save_and_get").bind("click", function(){
                saveAndGetCode();
				return false;
            });
            
        }
    });
}

function adsPreview(){

    $.fancybox.showActivity();
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/ads_preview",
        data: {
            ads_size: $("#ads_size").val(),
            title_color: $("#title_color").val(),
            desc_color: $("#desc_color").val(),
            border_color: $("#border_color").val(),
            background_color: $("#background_color").val(),
            category: $("#ad_category").val(),
            t: new Date().getTime()
        },
        success: function(data){
            $.fancybox(data);
        }
    });
    
    return false;
}

function adsSave(){
	$.fancybox.showActivity();
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/save_ads",
		//dataType: "json",
        data: {
            website: $("#online_website").val(),
			ads_name: $("#ads_name").val(),
			ads_size: $("#ads_size").val(),
			category: $("#ad_category").val(),
			title_color: $("#title_color").val(),
			desc_color: $("#desc_color").val(),
			border_color: $("#border_color").val(),
			background_color: $("#background_color").val()
        },
        success: function(data){
			$.fancybox(data);
			/*
			 * avoid multi save
			 */
			$("#ads_name").val('');
        }
    });
}

function saveAndGetCode(){
	$.fancybox.showActivity();
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/save_and_get_code",
        data: {
            website: $("#online_website").val(),
			ads_name: $("#ads_name").val(),
			ads_size: $("#ads_size").val(),
			category: $("#ad_category").val(),
			title_color: $("#title_color").val(),
			desc_color: $("#desc_color").val(),
			border_color: $("#border_color").val(),
			background_color: $("#background_color").val()
        },
        success: function(data){
			$.fancybox(data);
			/*
			 * avoid multi save
			 */
			$("#ads_name").val('');
        }
    });
}

function updateAds(){
    $.ajax({
		beforeSend: function(){
            $("#update_msg").html('<img src="/images/public/ajax-loader-circle.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/ads/update_ads",
        data: {
            param_type: $("#param_type").val(),
			ads_name: $("#ads_name").val(),
			category: $("#ad_category").val(),
			title_color: $("#title_color").val(),
			desc_color: $("#desc_color").val(),
			border_color: $("#border_color").val(),
			background_color: $("#background_color").val()
        },
        success: function(data){
			$("#update_msg").html(data);
			$("#"+$("#param_type").val()+"_ads_name").html($("#ads_name").val());
			//changeAdsWebSite($("#online_website"));
        }
    });
}

function deleteAds(website,ads,ads_name){
    if (confirm("確定要刪除： '"+ads_name+"' ? ")) {
		
        $.ajax({
            type: "POST",
            cache: false,
            url: "/ads/delete_ads",
            data: {
                website: website,
                ads: ads
            },
            success: function(data){
                changeAdsWebSite($("#online_website"));
            }
        });
        
    }
}

function getCode(param_type){
	$.fancybox.showActivity();
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/get_code",
        data: {
            param_type: param_type
        },
        success: function(data){
			$.fancybox(data);
        }
    });
}

function getUpdateAds(param_type){
	$.fancybox.showActivity();
    $.ajax({
        type: "POST",
        cache: false,
        url: "/ads/get_update_ads",
        data: {
            param_type: param_type
        },
        success: function(data){
			$.fancybox(data);
			$("#ads_update").bind("click", function(){
                updateAds();
				return false;
            });
        }
    });
}


function getWebSiteDetail(){
    $.ajax({
        beforeSend: function(){
            $("#detail_table").html('<img src="/images/public/ajax-loader.gif" />');
        },
        type: "POST",
        cache: false,
        url: "/report_detail/report_detail_table",
		dataType: "json",
        data: {
            website: $('#website').val(),
			start_date: $('#report_date_start').val(),
			end_date: $('#report_date_end').val(),
			chart_type: $("input[name='chart_type']:checked").val()
        },
        success: function(data){
            $("#detail_table").html(data['table_data']);
			if ($(".tablesorter").length > 0) {
				$(".tablesorter").tablesorter({
					sortList: [[0, 0]],
					widgets: ['zebra']
				});
			}
            plotOptions = {
                axes: {
                    xaxis: {
                        renderer: $.jqplot.DateAxisRenderer,
                        tickOptions: {
                            formatString: '%b&nbsp;%#d'
                        }
                    },
                    yaxis: {
						tickOptions: {
                        	formatString: '%d'
                    	},
                        max: data['max_data'],
                        min: data['min_data']
                    }
                },
                highlighter: {
                    show: true,
                    sizeAdjust: 7.5
                }
            };
			$.jqplot('dataChart', data['chart_data'], plotOptions).replot();
        }
    });
}